package com.gzhryc.shared_device.cdcz.core.protocol.v1.models;

import com.gzhryc.common.StringTools;
import com.gzhryc.shared_device.cdcz.core.protocol.v1.V1_CDCZConstants;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class PlugBKVData {

    private Map<String, BKVData> bkvDataMap;
    private PlugStateData stateData;

    public PlugBKVData(byte[] bytes) {
        this.bkvDataMap = new HashMap<>();
        this.analysisBKVData(bytes,0);
        BKVData bkvData = bkvDataMap.get(V1_CDCZConstants.KEY_PLUG_SLOT_STATE);
        stateData = new PlugStateData(bkvData.getHexValue());
    }

    private void analysisBKVData(byte[] contentBytes,int startNum){
        if(contentBytes.length > startNum) {
            int dataLength = Integer.valueOf(StringTools.byteArr2HexStr(new byte[]{contentBytes[startNum]}),16);
            byte[] data = Arrays.copyOfRange(contentBytes, startNum + 1, startNum + 1 + dataLength);
            int keyLength = Integer.valueOf(StringTools.byteArr2HexStr(new byte[]{data[0]}),16);
            byte[] keyBytes = Arrays.copyOfRange(data, 1, 1 + keyLength);
            byte[] valueBytes = Arrays.copyOfRange(data, keyLength + 1, dataLength);

            String key = StringTools.byteArr2HexStr(keyBytes).toUpperCase();

            BKVData bkvData = new BKVData();
            bkvData.setData(data);
            bkvData.setDataLength(dataLength);
            bkvData.setKeyLength(keyLength);
            bkvData.setKey(key);
            bkvData.setValue(valueBytes);
            this.bkvDataMap.put(bkvData.getKey(),bkvData);

            startNum = startNum + dataLength + 1;
            analysisBKVData(contentBytes, startNum);
        }
    }

    public Double getNowPower(){
        BKVData bkvData = bkvDataMap.get(V1_CDCZConstants.KEY_POWER);
        if(bkvData != null){
            return bkvData.getDoubleValue(1);
        }
        return null;
    }

    public Double getNowCurrent(){
        BKVData bkvData = bkvDataMap.get(V1_CDCZConstants.KEY_CURRENT);
        if(bkvData != null){
            return bkvData.getDoubleValue(3);
        }
        return null;
    }

    public Double getElectricity(){
        BKVData bkvData = bkvDataMap.get(V1_CDCZConstants.KEY_PLUG_ELECTRICITY);
        if(bkvData != null){
            return bkvData.getDoubleValue(3);
        }
        return null;
    }

    public Integer getChargeTime(){
        BKVData bkvData = bkvDataMap.get(V1_CDCZConstants.KEY_PLUG_CHARGE_TIME);
        if(bkvData != null){
            return bkvData.getIntegerValue();
        }
        return null;
    }

    public Integer getOverloadState(){
        return stateData.getOverload();
    }

    public Integer getOverCurrent(){
        return stateData.getOverCurrent();
    }

    public Integer getNotLoad(){
        return stateData.getNoLoad();
    }

    public Integer getTemperatureState(){
        return stateData.getOverTemperature();
    }

    public Integer getPowerState(){
        return stateData.getPowerState();
    }

    /**
     * 工作状态，1：空闲，2：工作，4：故障
     * @return
     */
    public Integer getWorkState(){
        int result = stateData.getWorkState();
        return result;
    }

    public Map<String,BKVData> getBKVDataMap(){
        return this.bkvDataMap;
    }
}
